package  com.lepu.app.fun.tool.ymsjb.remind;

import java.util.Random;

import com.app.utils.LoginUtil;
import com.eyzhs.app.R;
import com.lepu.app.main.activity.AppStartActivity;
import com.lepu.app.main.activity.MainTabActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

public class RemindReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		if(intent == null || TextUtils.isEmpty(intent.getAction()))
		{
			return;
		}

		Log.d("CUSTOM_TAG", "收到闹钟广播了    " + intent.getAction());

		if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
		{
			//设置最近的闹钟
			Alarm.setRemindNextAlarm(context);
		}else{

			Intent openIntent = null;
			if(MainTabActivity.getInstance() == null)
			{
				openIntent = new Intent(context, AppStartActivity.class);
			}else{
				openIntent = new Intent(context, MainTabActivity.class);
			}
			Alarm alarm = (Alarm) intent.getSerializableExtra("alarm");
			
			if(LoginUtil.checkIsLogin() && alarm.uid.equals(LoginUtil.getUserId())){
				showNotification(context, alarm.note, openIntent);
			}

//			Intent intentSound = new Intent(context, RemindSoundActivity.class);
			
//			Bundle bundle = new Bundle();
//			bundle.putSerializable("remindAlarm", alarm);
//			intentSound.putExtras(bundle);
//			intentSound.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			context.startActivity(intentSound);

		}
	}
	
	@SuppressWarnings("deprecation")
	public void showNotification(Context context, String info, Intent openIntent) {

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

		long when = System.currentTimeMillis();

		String title = context.getResources().getString(R.string.app_name);

		Notification notification = new Notification(R.drawable.app_icon,
				title, when);
		notification.defaults = Notification.DEFAULT_SOUND;// 发出默认声音
		notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知后自动清除通知

		RemoteViews rv = new RemoteViews(context.getPackageName(),
				R.layout.app_jqtx_notification);
		rv.setImageViewResource(R.id.ImageView, R.drawable.app_icon);
		rv.setTextViewText(R.id.notification_textview, info);
		PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt(),
				openIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 当点击消息时就会向系统发送openintent意图

		notification.contentView = rv;
		notification.contentIntent = contentIntent;
		notificationManager.notify(new Random().nextInt(), notification);// 第一个参数为自定义的通知唯一标识
	}
	
}